package quartz

import (
	"daxin.my/test-netserver/api"
	"github.com/robfig/cron"
)

var c *cron.Cron

func StartJob() {
	if c != nil {
		return
	}

	c = cron.New()

	for _, client := range api.MiddlewareClients {
		if client != nil {
			addCron(c, client)
		}
	}

	for _, client := range api.ProtocolClients {
		if client != nil {
			addCron(c, client)
		}
	}
	c.Start()
}

func StopJob() {
	if c == nil {
		return
	}

	c.Stop()
	c = nil
}

func addCron(c *cron.Cron, client api.Client) {
	if c == nil {
		return
	}
	c.AddFunc("*/1 * * * * ?", func() {
		client.Visit()
	})
}
